/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cmp.player.controls;

import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.renderer.RenderManager;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.control.AbstractControl;
import com.jme3.scene.control.BillboardControl;
import com.jme3.scene.control.Control;
import com.jme3.scene.shape.Quad;

/**
 * Handles getting hit and what happens when losing health and stuff.
 * @author krummer
 */
public class HealthControl extends AbstractControl {
    
    private static final float MAX_DEFEAT_TIME = 3.f;
    private float defeatTime;
    private Node healthBar;
    
    @Override
    public void setSpatial(Spatial spatial) {
        super.setSpatial(spatial);
        this.setHealth(100.f);
        spatial.setUserData("HealthReg", 5.f);
        spatial.setUserData("Defeated", false);
        this.defeatTime = 0.f;
        
        //create the healthbar
        this.healthBar = new Node("HPBar");
        /*BillboardControl billB = new BillboardControl();
        billB.setAlignment(BillboardControl.Alignment.Screen);
        this.healthBar.addControl(billB);
        
        Geometry box = new Geometry("HPBox", new Quad(10.f,2.f, false));
        Material mat = new Material();
        mat.setColor("HPcolor", ColorRGBA.Orange);
        this.healthBar.attachChild(box);
        box.move(Vector3f.UNIT_Y.mult(3.f));*/
        
        ((Node)spatial).attachChild(this.healthBar);
    }
    
    public void setHealth(Float value) {
        getSpatial().setUserData("Health", value);
    }
    
    public Float getHealth() {
        return getSpatial().getUserData("Health");
    }
    
    /**
     * @param amount of health to be removed
     */
    public void removeHealth(Float amount) {
        Float newHP = this.getHealth() - amount;
        
        if(newHP < 0.f) {
            this.setHealth(0.f);
        } else {
            this.setHealth(this.getHealth()-amount);
        }
        
    }
    
    public void setDefeated(boolean defeat) {
        this.getSpatial().setUserData("Defeated", defeat);
        if(defeat) {
            this.defeatTime = MAX_DEFEAT_TIME;
            System.out.println("DEFEAT!");
        } else {
            
        }
    }
    
    public boolean isDefeated() {
        return this.getSpatial().getUserData("Defeated");
    }

    @Override
    protected void controlUpdate(float tpf) {
        if(this.getHealth() <= 0) {
            this.setDefeated(true);
        } 
        
        if(this.isDefeated()) {
            this.defeatTime -= tpf;
            if(this.defeatTime <= 0.f) {
                this.setDefeated(false);
            }
        }        
    }

    @Override
    protected void controlRender(RenderManager rm, ViewPort vp) {
    }

    public Control cloneForSpatial(Spatial spatial) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
}
